package cn.yunhe.demo.factory;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDB {
    static Connection conn = null;
    static PreparedStatement psmt = null;
    static ResultSet rs = null;
    static String driverload;
    static String url;
    static String username;
    static String pass;

    /**
     * 获得链接
     *
     * @return
     */
    public static Connection getConn() {
        try {
            getD();
            Class.forName(driverload);
            conn = DriverManager.getConnection(url, username, pass);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

    /**
     * 获取配置文件
     *
     * @throws Exception
     */
    private static void getD() throws Exception {
        InputStream in = BaseDB.class.getClassLoader().getResourceAsStream("db.properties");
        Properties pro = new Properties();
        pro.load(in);
        driverload = pro.getProperty("driverload");
        url = pro.getProperty("url");
        username = pro.getProperty("username");
        pass = pro.getProperty("pass");
    }

    /**
     * 关闭所有
     *
     * @param conn
     * @param st
     * @param rs
     */
    public static void clossAll(Connection conn, Statement st, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
